<?php

/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
namespace app\commands;

use yii\console\Controller;
use app\service\models as models;
use yii;

/**
 * This command echoes the first argument that you have entered.
 *
 * This command is provided as an example for you to learn how to create console commands.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class RecognitionController extends Controller
{
    public $dsRec;
    
    public $dsImage;
    
    private $_recCountRedisKey = 'oct|recCountRedisKey';
    /**
     * 扫描全表检测识别结果
     */
    public function actionRecognition()
    {
        set_time_limit(0);
        $dsRec = new models\Recognition();
        $dsimage = new models\Image();
        while (true) {
            try {
                //添加任务
                $listtask = $dsRec->find()->where(['status'=>0])->all();
                foreach ($listtask as $key=>$value) {
                    $count = Yii::$app->cache->get($this->_recCountRedisKey);
                    if(!$count) {
                        Yii::$app->cache->set($this->_recCountRedisKey, 0);
                        $count = 0;
                    }
                    if($count >= 2) { //设置最多两个用户同时执行
                        break;
                    }
                    Yii::$app->cache->incr($this->_recCountRedisKey);
            
                    $imageret = $dsimage->find()->where(['id'=>$value->image_id])->one();
                    $imageret->status = 1;
                    $imageret->save();
            
                    $srcPath = $imageret->real_path;
                    $retPath = $value->real_path;
            
                    //修改状态为识别中
                    $value->status = 1;
                    $value->save();
            
                    $ret = exec("cd /root/matlab  && nohup ./main {$srcPath} {$retPath} >> /dev/null &", $op);
                    error_log(print_r($op, true).'\n', 3, '/root/matlab/error.log');
                }
            
                //检测任务是否执行完毕
                $list = $dsRec->find()->where(['status'=>1])->all();
                foreach ($list as $value) {
                    $retPath = $value->real_path;
                    //程序尚未执行完
                    if (!file_exists($retPath)) {
                        continue;
                    }
            
                    //检测是否病变
                    if(file_exists($retPath.'.1')) {
                        $value->recognition_ret = 1;
                    } else {
                        $value->recognition_ret = 0;
                    }
            
                    $value->status = 2;
                    $value->save();
            
                    $imageins = $dsimage->find()->where(['id'=>$value->image_id])->one();
                    if($imageins) {
                        $imageins->status = 2;
                        $imageins->save();
                    }
            
                    Yii::$app->cache->decr($this->_recCountRedisKey);
                }
            } catch (\Exception $e) {
                var_dump($e->getMessage());
            }
            sleep(5);
        }
    }
}
